programming4us
           
 
 
Programming

User-Level Security : Custom Authentication

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
10/17/2010 6:02:51 PM
Although the options that WCF offers for authentication are helpful, there are always gaps through which specific requirements will fall. It is not possible to guarantee that all the available choices will cover every possible scenario, so in the typical WCF manner, you can extend the authentication process with your own custom mechanism. This section describes the process for doing this, along with some of the ramifications.

First, to use custom authentication, the client credential type must be set to UserName. This enables the username and password to be submitted to the service to perform the authentication. A side effect is that with a UserName client credential type, WCF requires the service to reference a certificate that contains a public/private key pair. The public key portion of the certificate then encrypts the credentials before they are transmitted to the service.

The default, when the client credential type is UserName, is for the service to use Windows to perform the authentication. To intercept this process, the starting point is to create a class that derives from the UserNamePasswordValidator class, which is in the System.IdentityModel.Selectors namespace. Within this class, the authentication mechanism is introduced into the process by overriding the Validate method, which is involved when WCF is in the middle of authentication.

Probably the most interesting aspect of the Validate method is that it doesn’t return a Boolean value. In fact, this method call doesn’t return any value at all. If the method completes, WCF assumes that the credentials were valid. To invalidate the credentials, a SecurityTokenValidationException exception must be raised. An example of such a class can be seen in the following code:

' VB
Public Class CustomAuthenticator
Inherits UserNamePasswordValidator

Public Overrides Sub Validate(userName As String, password As String)
If (userName <> "anyuser" OR password <> "good") Then
Throw New SecurityTokenValidationException("Invalid credentials")
End If
End Sub

End Class

// C#
public class CustomAuthenticator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (userName != "anyuser" || password != "good")
throw new SecurityTokenValidationException("Invalid credentials");
}
}


After the Validator class has been created, the next step is to configure the service to use its functionality. You do this by specifying the validator type as part of the service’s behavior configuration. The following segment from a configuration file defines a service behavior that does just this:

<serviceBehaviors>
<behavior name="CustomValidator">
<serviceCredentials>
<userNameAuthentication
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType=
"ThisAssembly.CustomAuthenticator, ThisAssembly"/>
<serviceCertificate
findValue="localhost" x509FindType="FindBySubjectName"
storeLocation="CurrentUser" storeName="My" />
</serviceCredentials>
</behavior>
</serviceBehaviors>

The userNameAuthentication element contains the details that specify the custom authentication module. Although the serviceCertificate element doesn’t have anything to do directly with the custom authentication, it is one of the techniques you can use to provide the certificate information to encode the credentials for transmission.

Note also that the behavior just defined is not a part of any service by default. The service must specify this behavior by setting the behaviorConfiguration attribute to the name of the behavior (CustomValidator in this example). Also, the client must ensure that the client credential type is set to UserName.

To actually provide the credentials requires just a small piece of coding on the client side. If you are using the ChannelFactory class to create a proxy, the following code will submit a set of credentials with the request, and it will use the configuration information associated with the endpoint that has a name attribute of DemoEndpoint:

' VB
Dim factory As New _
ChannelFactory(Of IUpdateService)("DemoEndpoint")
factory.Credentials.UserName.UserName = "anyuser"
factory.Credentials.UserName.Password = "good"

// C#
ChannelFactory<IUpdateService> factory =
new ChannelFactory<IUpdateService>("DemoEndpoint");
factory.Credentials.UserName.UserName = "anyuser";
factory.Credentials.UserName.Password = "good";


Note: When using custom authentication, you cannot specify the username and password automatically through configuration. The credentials must be assigned explicitly.

It is possible that you might have to specify a Domain Name System (DNS) identity to use the certificate. This might be necessary if the client authenticates the service’s certificate prior to sending a request. This type of problem is indicated through a message similar to the following:

Identity check failed for outgoing message. The expected DNS identity of the remote endpoint
was 'X' but the remote endpoint provided DNS claim 'Y'. If this is a legitimate remote
endpoint, you can fix the problem by explicitly specifying DNS identity 'Y' as the Identity
property of EndpointAddress when creating channel proxy.


The solution, as the message quite nicely suggests, is to set the identity explicitly for the DNS. You can do this by adding an identity element to the endpoint definition within the client’s configuration file, as shown here:

<identity>
<dns value="Y"/>
</identity>
Other -----------------
- User-Level Security : Authorization and Impersonation (part 4) - Impersonation
- User-Level Security : Authorization and Impersonation (part 3) - Security Token Authentication
- User-Level Security : Authorization and Impersonation (part 2) - Claims-Based Authorization
- User-Level Security : Authorization and Impersonation (part 1) - Authorization
- Publisher Certificates
- Using LINQ To SQL
- Service Management API (part 2) - Making API Requests
- Service Management API (part 1)
- Windows Services : A Service Control Shell
- ASP.NET Applications and the Web Server
- Internet Information Services (IIS)
- Managing Websites with IIS Manager (part 7) - Confidentiality with SSL and Certificates
- Managing Websites with IIS Manager (part 6) - The Machine Key and Windows Authentication
- Managing Websites with IIS Manager (part 5) - The Default Page and Custom Error Pages
- Managing Websites with IIS Manager (part 4) - Configuration
- Managing Websites with IIS Manager (part 3) - The ASP.NET Account
- Managing Websites with IIS Manager (part 2) - Understanding Application Pools
- Managing Websites with IIS Manager (part 1) - Creating a Virtual Directory
- Deploying ASP.NET 4 Applications with Visual Studio (part 2) - Copying a Website and Publishing a Website
- Deploying ASP.NET 4 Applications with Visual Studio (part 1) - Creating a Virtual Directory for a New Project
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us